package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import po.EmpHistory;
import po.Rating;
import po.User;
import service.IHistoryService;
import service.UserService;

public class HistoryAction {
	private IHistoryService historyService;
	private List<EmpHistory> list;
	public void setHistoryService(IHistoryService historyService) {
		this.historyService = historyService;
	}
	
	public String getAllHistory() {
		int id;
		HttpServletRequest request = ServletActionContext.getRequest();	
		HttpSession session = request.getSession();
		id = (int) session.getAttribute("userID");
		list = historyService.getAllHistory(id);
		//return "success";
		
		int userID = (int) session.getAttribute("userID");
		UserService us = new UserService();
		if(us.isTutor(userID)) {
			return "success";
		}
		request.setAttribute("tip", "请先提交相应材料注册家教！");
		return "noright"; 
	}
	private int hisID;   
	   
	public void getMoreInfo() {
		HttpServletResponse response = ServletActionContext.getResponse();
		User user = historyService.getTutorByHisID(hisID);
		Rating rat = user.getRat();
		EmpHistory history = historyService.getHisByID(hisID);
		PrintWriter out;
		
		try {  
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();		
			out.println("课程的信息："+"<br>");
			out.println("详情："+history.getNeedOrPresentation()+"<br>");
			out.println("<br>");
			out.println("授课家教的信息："+"<br>");
			out.println("账户名："+user.getAccount()+"<br>");
			out.println("联系方式："+user.getUserDetail().getPhone()+"<br>");	
			out.println("电子邮箱："+user.getUserDetail().getEmail()+"<br>");
			//完善评价系统后修改
			out.println("完成课程数："+rat.getNumber()+"<br>");
			out.println("用户评价："+"<span style=\"color:green\">"+"好评数："+rat.getGoodMark()+"  </span>"+"<span style=\"color:red\">"+"  差评数:"+rat.getBadMark()+"</span>"+"<br>");
			out.println("综合评分:"+rat.getAvgMark()+"<br>");
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}  

	public void getAppriseInfo() {
		HttpServletResponse response = ServletActionContext.getResponse();
		User user = historyService.getUserByHisID(hisID);
		EmpHistory history = historyService.getHisByID(hisID);
		PrintWriter out;
		
		try {  
			response.setContentType("text/html");
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();		
			out.println("课程的信息："+"<br>");
			out.println("详情："+history.getNeedOrPresentation()+"<br>");
			out.println("用户评价："+history.getAppraise()+"<br>");
			out.println("用户给分："+history.getMark()+"<br>");
			out.println("<br>");
			out.println("用户的信息："+"<br>");
			out.println("账户名："+user.getAccount()+"<br>");
			out.println("联系方式："+user.getUserDetail().getPhone()+"<br>");	
			out.println("电子邮箱："+user.getUserDetail().getEmail()+"<br>");
			
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
	}
	private String mark;
	private String appraise;
	
	//首次评价
	public void firstAppraise() {
		historyService.update(hisID, appraise, mark, 1);
	}
	//再次评价
	public void laterAppraise() {
		historyService.update(hisID, appraise, null, 0);
	}
	
	
	
	public List<EmpHistory> getList() {
		return list;
	}

	public void setList(List<EmpHistory> list) {
		this.list = list;
	}

	public int getHisID() {
		return hisID;
	}

	public void setHisID(int hisID) {  
		this.hisID = hisID;
	}

	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public String getAppraise() {
		return appraise;
	}

	public void setAppraise(String appraise) {
		this.appraise = appraise;
	}
}
